/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

var obtainedId;
var obtainedIdKPI;
jQuery(document).ready(function()
{

	$.ajax({
		url : 'cargaVistaImpoExpo', // action to be perform
		dataType : 'html',
		success : function(html) {
			$('#vistaImpo').html(html); // set result.jsp output to leftDiv
		},
		error : function(xhr, ajaxOptions, thrownError) {
			alert('An error occurred! ' + thrownError);
		},
	});
	

//	
//	$.ajax({
//		url : 'getContacts', // action to be perform
//		dataType : 'html',
//		success : function(html) {
//			$('#copContacts').html(html); // set result.jsp output to leftDiv
//		},
//		error : function(xhr, ajaxOptions, thrownError) {
//			alert('An error occurred! ' + thrownError);
//		},
//	});
//	
//	$.ajax({
//		url : 'getScopeService', // action to be perform
//		dataType : 'html',
//		success : function(html) {
//			$('#scopeService').html(html); // set result.jsp output to leftDiv
//		},
//		error : function(xhr, ajaxOptions, thrownError) {
//			alert('An error occurred! ' + thrownError);
//		},
//	});
//	
//	$.ajax({
//		url : 'getWorkingInstructions', // action to be perform
//		dataType : 'html',
//		success : function(html) {
//			$('#"copWorkingInstuctions"').html(html); // set result.jsp output to leftDiv
//		},
//		error : function(xhr, ajaxOptions, thrownError) {
//			alert('An error occurred! ' + thrownError);
//		},
//	});
//	

//	
	

	
	$.ajax({
		url : 'getImportList', // action to be perform
		dataType : 'html',
		success : function(html) {
			$('#listImports').html(html); // set result.jsp output to leftDiv
		},
		error : function(xhr, ajaxOptions, thrownError) {
			alert('An error occurred! ' + thrownError);
		},
	});
	
	
	$.ajax({
		url : 'getExportList', // action to be perform
		dataType : 'html',
		success : function(html) {
			$('#listExports').html(html); // set result.jsp output to leftDiv
		},
		error : function(xhr, ajaxOptions, thrownError) {
			alert('An error occurred! ' + thrownError);
		},
	});
	
});

//var obtieneId = obtieneId(id){
//	alert(id);
//	obtainedId = id;
//};

var obtieneId = function(id){
	obtainedId = id;
	obtieneOverview();
	obtieneScopeService();
	cargaContacts();
	obtieneScopeService();
	insertaCamposWI();
//	obtieneWorkingInstructions();
	obtieneBillingInstruction();
	obtieneKPI();
	obtieneExceptionCodes();
	obtieneRequirements();
	obtieneReports();
	obtieneAttachment();
	obtieneListaAdjuntos();
	
};

//var obtieneIdKPI = function(idKPI){
//	obtainedIdKPI = idKPI;
//	updateKPIFields();
//}

var insertaCamposWI = function(){
	$.ajax({
		url : 'insertWIFields', // action to be perform
		dataType : 'html',
		data : {
			idImportExport : obtainedId,
		},
		success : function(html) {
			$('#copWorkingInstructions').html(html);
		},
		error : function(xhr, ajaxOptions, thrownError) {
			alert('An error occurred! ' + thrownError);
		},
	});
};

var obtieneOverview = function(){
	$.ajax({
		url : 'getFormOverview', // action to be perform
		dataType : 'html',
		data : {
			idImportExport : obtainedId,
		},
		success : function(html) {
			$('#clientOverview').html(html);
		},
		error : function(xhr, ajaxOptions, thrownError) {
			alert('An error occurred! ' + thrownError);
		},
	});	
};

var obtieneScopeService = function(){
	$.ajax({
		url : 'getFormScopeService', // action to be perform
		dataType : 'html',
		data : {
			idImportExport : obtainedId,
		},
		success : function(html) {
			$('#copScopeService').html(html);
		},
		error : function(xhr, ajaxOptions, thrownError) {
			alert('An error occurred! ' + thrownError);
		},
	});	
};

var obtieneWorkingInstructions = function(){
	$.ajax({
		url : 'getFormWorkingInstructions', // action to be perform
		dataType : 'html',
		data : {
			idImportExport : obtainedId,
		},
		success : function(html) {
			$('#copWorkingInstructions').html(html);
		},
		error : function(xhr, ajaxOptions, thrownError) {
			alert('An error occurred! ' + thrownError);
		},
	});	
};

var obtieneBillingInstruction = function(){
	$.ajax({
	url : 'getBillingInstructions', // action to be perform
	dataType : 'html',
	success : function(html) {
		$('#copBillingInstructions').html(html); // set result.jsp output to leftDiv
	},
	error : function(xhr, ajaxOptions, thrownError) {
		alert('An error occurred! ' + thrownError);
	},
});
} ;

var obtieneKPI= function (){
	$.ajax({
	url : 'getKPI', // action to be perform
	dataType : 'html',
	success : function(html) {
		$('#copKPI').html(html); // set result.jsp output to leftDiv
	},
	error : function(xhr, ajaxOptions, thrownError) {
		alert('An error occurred! ' + thrownError);
	},
});
};


var obtieneExceptionCodes = function (){
	$.ajax({
	url : 'getExceptionCodes', // action to be perform
	dataType : 'html',
	success : function(html) {
		$('#copExceptionCodes').html(html); // set result.jsp output to leftDiv
	},
	error : function(xhr, ajaxOptions, thrownError) {
		alert('An error occurred! ' + thrownError);
	},
});
};

var obtieneRequirements = function (){
	$.ajax({
	url : 'getRequirementsCop', // action to be perform
	dataType : 'html',
	success : function(html) {
		$('#copRequirements').html(html); // set result.jsp output to leftDiv
	},
	error : function(xhr, ajaxOptions, thrownError) {
		alert('An error occurred! ' + thrownError);
	},
});
};

var obtieneReports = function (){
	$.ajax({
	url : 'getReports', // action to be perform
	dataType : 'html',
	success : function(html) {
		$('#copReports').html(html); // set result.jsp output to leftDiv
	},
	error : function(xhr, ajaxOptions, thrownError) {
		alert('An error occurred! ' + thrownError);
	},
});
};

var obtieneAttachment = function (){
	$.ajax({
	url : 'getAttachments', // action to be perform
	dataType : 'html',
	success : function(html) {
		$('#copAttachments').html(html); // set result.jsp output to leftDiv
	},
	error : function(xhr, ajaxOptions, thrownError) {
		alert('An error occurred! ' + thrownError);
	},
});
};

var obtieneKPISpeial = function (){
	$.ajax({
		url : 'getKpiSpecial', // action to be perform
		dataType : 'html',
		success : function(html) {
			$('#copKPISpecial').html(html); // set result.jsp output to leftDiv
		},
		error : function(xhr, ajaxOptions, thrownError) {
			alert('An error occurred! ' + thrownError);
		},
	});
};

var saveCop = function(){
	$.ajax({
		url : 'submitForm', // action to be perform
		dataType : 'html',
		data : {
			idImportExport : obtainedId,
			overviewhidden : $("#copClientOverview").val(),
			fclScope : $("#cbg11s").prop('checked'),
			lclScope : $("#cbg12s").prop('checked'),
			perishablesScope : $("#cbg13s").prop('checked'),
	    	brokerageScope : $("#cbg14s").prop('checked'),
	    	intermodalScope : $("#cbg15s").prop('checked'),
	    	insuranceScope : $("#cbg16s").prop('checked'),
	    	oceanFreightScope :  $("#cbg17s").prop('checked'),
		    blueAnchoLineScope :  $("#cbg18s").prop('checked'),
		    seaTransportScope :  $("#cbg19s").prop('checked'),
		    exportCustomScope :  $("#cbg20s").prop('checked'),
		    inbondExpScope :  $("#cbg21s").prop('checked'),
		    importCustomScope :  $("#cbg22s").prop('checked'),
		    inbondImpScope :  $("#cbg23s").prop('checked'),
		    preCarrierScope :  $("#cbg24s").prop('checked'),
		    allMotorScope :  $("#cbg25s").prop('checked'),
		    combinedScope :  $("#cbg26s").prop('checked'),
		    onCarriageScope :  $("#cbg27s").prop('checked'),
		    pickUpToActDep : $("#pickToActDep").prop('checked'),
		    custClearToActDep : $("#CustToActDep").prop('checked'),
		    actDepToActArriv : $("#ActDepToActArr").prop('checked'),
		    actArrivToCustClear : $("#ActArrToCust").prop('checked'),
		    actArrivToDelivery : $("#ActArrToDev").prop('checked'),
//		    workingInst : $("#workingInstructionsValue").val(),
		    requirementsCop : $("#requirementsValue").val(),
		},
		success : function(html) {
//			$('#contenidoInformacion').html(html); // set result.jsp output to leftDiv
		},
		error : function(xhr, ajaxOptions, thrownError) {
			alert('An error occurred! ' + thrownError);
		},
	});
	
};

var changeCheckKPI = function(obtainedIdKPI){
	var elem = document.getElementById(obtainedIdKPI);
	var childNodes = elem.childNodes;
	var valorCheck = childNodes[3].value;
	var comodin = '';
	
	if(valorCheck == 'false'){
		comodin = 'true';
	}		
	else if(valorCheck == 'true'){
		comodin = 'false';	
	}	
	
	
	$.ajax({
		url: 'updateKPICheckBox',
		dataType : 'html',
		data : {
			idKPI : obtainedIdKPI,
			checkBoxKPI : comodin,
		},
		success : function(html) {
			$('#copKPI').html(html); // set result.jsp output to leftDiv
		},
		error : function(xhr, ajaxOptions, thrownError) {
			alert('An error occurred! ' + thrownError);
		},
	});
	
};

var updateWIFields = function(obtainedIdKPI){	
	var elem = document.getElementById(obtainedIdKPI);
	var childNodes = elem.childNodes;
	var valorText;
	for(var i=0; i<childNodes.length; i++) {
		if(i==3){
			valorText = childNodes[i].value;
		}			
//		else if(i==5){
//			alert(childNodes[i].value);
//			valorText = childNodes[i].value; 
//		}			
	}
//	var child = elem.children()[1].value;
//	var childval = child.getElementsByTagName("input").value;
//	alert(child);
	$.ajax({
		url: 'updateWIFields',
		dataType : 'html',
		data : {
			idWorkInst : obtainedIdKPI,
			valueWorkInst : valorText,
		},
		success : function(html) {
			$('#copWorkingInstructions').html(html); // set result.jsp output to leftDiv
		},
		error : function(xhr, ajaxOptions, thrownError) {
			alert('An error occurred! ' + thrownError);
		},
	});
};

var updateKPIFields = function(obtainedIdKPI){	
	var elem = document.getElementById(obtainedIdKPI);
	var childNodes = elem.childNodes;
	var valorCheck;
	var valorText;
	for(var i=0; i<childNodes.length; i++) {
		if(i==5){
			valorText = childNodes[i].value;
		}			
//		else if(i==5){
//			alert(childNodes[i].value);
//			valorText = childNodes[i].value; 
//		}			
	}
//	var child = elem.children()[1].value;
//	var childval = child.getElementsByTagName("input").value;
//	alert(child);
	$.ajax({
		url: 'updateKPI',
		dataType : 'html',
		data : {
			idKPI : obtainedIdKPI,
//			checkBoxKPI : valorCheck,
			valueKPI : valorText,
		},
		success : function(html) {
			$('#copKPI').html(html); // set result.jsp output to leftDiv
		},
		error : function(xhr, ajaxOptions, thrownError) {
			alert('An error occurred! ' + thrownError);
		},
	});
};

var addKPI = function(){
	$.ajax({
		url : 'addKPI', // action to be perform
		dataType : 'html',
		success : function(html) {
			$('#copKPI').html(html); // set result.jsp output to leftDiv
		},
		error : function(xhr, ajaxOptions, thrownError) {
			alert('An error occurred! ' + thrownError);
		},
	});
	
};

var addImport = function(){
	$.ajax({
		url : 'addImport', // action to be perform
		dataType : 'html',
		success : function(html) {
			cargarImports();
		},
		error : function(xhr, ajaxOptions, thrownError) {
			alert('An error occurred! ' + thrownError);
		},
	});
	
};

var addExport = function(){
	$.ajax({
		url : 'addExport', // action to be perform
		dataType : 'html',
		success : function(html) {
			cargarExports();
		},
		error : function(xhr, ajaxOptions, thrownError) {
			alert('An error occurred! ' + thrownError);
		},
	});
	
};

var cargarImports = function (){
	$.ajax({
		url : 'getImportList', // action to be perform
		dataType : 'html',
		success : function(html) {
			$('#listImports').html(html); // set result.jsp output to leftDiv
		},
		error : function(xhr, ajaxOptions, thrownError) {
			alert('An error occurred! ' + thrownError);
		},
	});
};

var cargarExports = function (){
	$.ajax({
		url : 'getExportList', // action to be perform
		dataType : 'html',
		success : function(html) {
			$('#listExports').html(html); // set result.jsp output to leftDiv
		},
		error : function(xhr, ajaxOptions, thrownError) {
			alert('An error occurred! ' + thrownError);
		},
	});
};


var cargaContacts = function(){
	$.ajax({
	url : 'getContacts', // action to be perform
	dataType : 'html',
	success : function(html) {
		$('#copContacts').html(html); // set result.jsp output to leftDiv
	},
	error : function(xhr, ajaxOptions, thrownError) {
		alert('An error occurred! ' + thrownError);
	},
});
};


var muestraPopUpAddContacts = function(){
	$('#addContact').bPopup();
};

function clickBoxFcl() {
    alert("test");

    var ff = $("#cbg21s").attr("checked");
    if (ff == "checked") {
        alert("cambiooo");
        $("#cbg21s").attr("checked", "On");
    } else {
        $("#cbg21s").attr("checked", false);
        alert("false cambio");
    }
};


function submitGenerlForm(idoverview, idoriginconditions, iddestconditions, idworkinst, idrequirements, idprotocol) {

    $("#txtareaoverviewhidden").val(idoverview);
    $("#txtareaoriginconditionsHIDEN").val(idoriginconditions);
    $("#txtareadestconditionsHIDEN").val(iddestconditions);
    $("#txtareaworkinstHIDEN").val(idworkinst);
    $("#txtarearequirementsHIDEN").val(idrequirements);
    $("#txtareaprotocolHIDEN").val(idprotocol);

    var valor = $("#cbg21s").attr("checked");

    if (valor == "checked") {
        $("#cbg21h").attr("checked", "On");
    } else {
        $("#cbg21h").attr("checked", false);
    }

    valor = $("#cbg22s").attr("checked");

    if (valor == "checked") {
        $("#cbg22h").attr("checked", "On");
    } else {
        $("#cbg22h").attr("checked", false);
    }

    valor = $("#cbg23s").attr("checked");

    if(valor == "checked") {
        $("#cbg23h").attr("checked", "On");
    } else {
        $("#cbg23h").attr("checked", false);
    }

//valor = $("#cbg24s").attr("checked");
//if(valor=="checked"){
//    $("#cbg24h").attr("checked", "On");
//}else{
//    $("#cbg24h").attr("checked", false);
//}

    valor = $("#cbg31s").attr("checked");
    if (valor == "checked") {
        $("#cbg31h").attr("checked", "On");
    } else {
        $("#cbg31h").attr("checked", false);
    }

    valor = $("#cbg32s").attr("checked");
    if (valor == "checked") {
        $("#cbg32h").attr("checked", "On");
    } else {
        $("#cbg32h").attr("checked", false);
    }

    valor = $("#cbg33s").attr("checked");
    if (valor == "checked") {
        $("#cbg33h").attr("checked", "On");
    } else {
        $("#cbg33h").attr("checked", false);
    }

    valor = $("#cbg34s").attr("checked");
    if (valor == "checked") {
        $("#cbg34h").attr("checked", "On");
    } else {
        $("#cbg34h").attr("checked", false);
    }

    valor = $("#cbg41s").attr("checked");
    if (valor == "checked") {
        $("#cbg41h").attr("checked", "On");
    } else {
        $("#cbg41h").attr("checked", false);
    }

    valor = $("#cbg42s").attr("checked");
    if (valor == "checked") {
        $("#cbg42h").attr("checked", "On");
    } else {
        $("#cbg42h").attr("checked", false);
    }

    valor = $("#cbg43s").attr("checked");
    if (valor == "checked") {
        $("#cbg43h").attr("checked", "On");
    } else {
        $("#cbg43h").attr("checked", false);
    }

    valor = $("#cbg44s").attr("checked");
    if (valor == "checked") {
        $("#cbg44h").attr("checked", "On");
    } else {
        $("#cbg44h").attr("checked", false);
    }

    valor = $("#cbg45s").attr("checked");
    if (valor == "checked") {
        $("#cbg45h").attr("checked", "On");
    } else {
        $("#cbg45h").attr("checked", false);
    }

    valor = $("#cbg46s").attr("checked");
    if (valor == "checked") {
        $("#cbg46h").attr("checked", "On");
    } else {
        $("#cbg46h").attr("checked", false);
    }

    valor = $("#cbg51s").attr("checked");
    if (valor == "checked") {
        $("#cbg51h").attr("checked", "On");
    } else {
        $("#cbg51h").attr("checked", false);
    }

    valor = $("#cbg52s").attr("checked");
    if (valor == "checked") {
        $("#cbg52h").attr("checked", "On");
    } else {
        $("#cbg52h").attr("checked", false);
    }

    valor = $("#cbg53s").attr("checked");
    if (valor == "checked") {
        $("#cbg53h").attr("checked", "On");
    } else {
        $("#cbg53h").attr("checked", false);
    }

    valor = $("#cbg54s").attr("checked");
    if (valor == "checked") {
        $("#cbg54h").attr("checked", "On");
    } else {
        $("#cbg54h").attr("checked", false);
    }

    valor = $("#cbg55s").attr("checked");
    if (valor == "checked") {
        $("#cbg55h").attr("checked", "On");
    } else {
        $("#cbg55h").attr("checked", false);
    }

    valor = $("#cbg61s").attr("checked");
    if (valor == "checked") {
        $("#cbg61h").attr("checked", "On");
    } else {
        $("#cbg61h").attr("checked", false);
    }

    valor = $("#cbg62s").attr("checked");
    if (valor == "checked") {
        $("#cbg62h").attr("checked", "On");
    } else {
        $("#cbg62h").attr("checked", false);
    }


    $("#GeneralForm").submit();
};


function asignarNomfile() {
    var nombre = $("#idInputFile").val();
    //alert(nombre);
    $('#idNomFile').val(nombre);
};


var agregaContact = function(){

	$.ajax({
		url : 'addContact', // action to be perform 
		type : 'POST',
		data : {
			name : $('#inputName').val(),
			position : $('#inputPosition').val(),
			company : $('#inputCompany').val(),
			email : $('#inputEmail').val(),
			phone : $('#inputPhone').val(),
			ext : $('#inputExt').val(),
			mobile : $('#inputMobile').val(),
			
		},
		dataType : 'html',
		success : function(html) {
			$('#copContacts').html(html);
		},
		error : function(xhr, ajaxOptions, thrownError) {
			alert('An error occurred! ' + thrownError);
		},
	});
};

var agregaExceptionCode = function(){

	$.ajax({
		url : 'addException', // action to be perform 
		type : 'POST',
		data : {
			exceptionCode : $('#inputExceptionCode').val(),
			exceptionLogic : $('#inputExceptionLogic').val(),
			descriptionException : $('#inputDescriptionException').val()
			
			
		},
		dataType : 'html',
		success : function(html) {
			obtieneExceptionCodes();
			$('#copExceptionCodes').html(html);
		},
		error : function(xhr, ajaxOptions, thrownError) {
			alert('An error occurred! ' + thrownError);
		},
	});
};


var obtieneListaAdjuntos = function(){

	$.ajax({
		url : 'getListAttachment', // action to be perform 
		type : 'POST',
		dataType : 'html',
		data : {
			idImportExport : obtainedId,
		},
		success : function(html) {
			$('#listFilesCop').html(html);
		},
		error : function(xhr, ajaxOptions, thrownError) {
			alert('An error occurred! ' + thrownError);
		},
	});
};


var eliminaAdjunto = function(fileName, idDeleteFile){
	$.ajax({
		url : 'deleteFileCop', // action to be perform 
		data : {
			nameFileDownload : fileName,
			idDeleteFileCop : idDeleteFile

		},
		type : 'POST',
		dataType : 'html',
		success : function(html) {
			$('#listFilesCop').html(html);
		},
		error : function(xhr, ajaxOptions, thrownError) {
			alert('An error occurred! ' + thrownError);
		},
	});
};

var eliminaContacCop = function(idContact){
	$.ajax({
		url : 'deleteContactCop', // action to be perform 
		data : {
			idContactDel : idContact,
		},
		type : 'POST',
		dataType : 'html',
		success : function(html) {
			$('#copContacts').html(html);
		},
		error : function(xhr, ajaxOptions, thrownError) {
			alert('An error occurred! ' + thrownError);
		},
	});
};


var notificarStatusCop = function() {

	$.ajax({
		url : 'notifyStatusCop', // action to be perform 
		type : 'POST',
		dataType : 'html',
		success : function(html) {
			alert("se ha realizado la notificacion");
		},
		error : function(xhr, ajaxOptions, thrownError) {
			alert('An error occurred! ' + thrownError);
		},
	});
};

